<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="客户id" prop="clientId">
      <el-input v-model="dataForm.clientId" placeholder="客户id"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="clientName">
      <el-input v-model="dataForm.clientName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="结算时间" prop="fundsOrderDate">
      <el-input v-model="dataForm.fundsOrderDate" placeholder="结算时间"></el-input>
    </el-form-item>
    <el-form-item label="账户ID" prop="accountId">
      <el-input v-model="dataForm.accountId" placeholder="账户ID"></el-input>
    </el-form-item>
    <el-form-item label="账户名称" prop="accountName">
      <el-input v-model="dataForm.accountName" placeholder="账户名称"></el-input>
    </el-form-item>
    <el-form-item label="贷款金额" prop="remainAmount">
      <el-input v-model="dataForm.remainAmount" placeholder="贷款金额"></el-input>
    </el-form-item>
    <el-form-item label="销售单" prop="saleOrder">
      <el-input v-model="dataForm.saleOrder" placeholder="销售单"></el-input>
    </el-form-item>
    <el-form-item label="销售总金额" prop="saleAmount">
      <el-input v-model="dataForm.saleAmount" placeholder="销售总金额"></el-input>
    </el-form-item>
    <el-form-item label="当前销货金额" prop="writtenOffAmount">
      <el-input v-model="dataForm.writtenOffAmount" placeholder="当前销货金额"></el-input>
    </el-form-item>
    <el-form-item label="当前剩余金额" prop="thisRemainAmount">
      <el-input v-model="dataForm.thisRemainAmount" placeholder="当前剩余金额"></el-input>
    </el-form-item>
    <el-form-item label="当前销贷次数" prop="thisWrittenOffNum">
      <el-input v-model="dataForm.thisWrittenOffNum" placeholder="当前销贷次数"></el-input>
    </el-form-item>
    <el-form-item label="共计销贷次数" prop="writtenOffNum">
      <el-input v-model="dataForm.writtenOffNum" placeholder="共计销贷次数"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          clientId: '',
          clientName: '',
          fundsOrderDate: '',
          accountId: '',
          accountName: '',
          remainAmount: '',
          saleOrder: '',
          saleAmount: '',
          writtenOffAmount: '',
          thisRemainAmount: '',
          thisWrittenOffNum: '',
          writtenOffNum: '',
          remark: ''
        },
        dataRule: {
          clientId: [
            { required: true, message: '客户id不能为空', trigger: 'blur' }
          ],
          clientName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          fundsOrderDate: [
            { required: true, message: '结算时间不能为空', trigger: 'blur' }
          ],
          accountId: [
            { required: true, message: '账户ID不能为空', trigger: 'blur' }
          ],
          accountName: [
            { required: true, message: '账户名称不能为空', trigger: 'blur' }
          ],
          remainAmount: [
            { required: true, message: '贷款金额不能为空', trigger: 'blur' }
          ],
          saleOrder: [
            { required: true, message: '销售单不能为空', trigger: 'blur' }
          ],
          saleAmount: [
            { required: true, message: '销售总金额不能为空', trigger: 'blur' }
          ],
          writtenOffAmount: [
            { required: true, message: '当前销货金额不能为空', trigger: 'blur' }
          ],
          thisRemainAmount: [
            { required: true, message: '当前剩余金额不能为空', trigger: 'blur' }
          ],
          thisWrittenOffNum: [
            { required: true, message: '当前销贷次数不能为空', trigger: 'blur' }
          ],
          writtenOffNum: [
            { required: true, message: '共计销贷次数不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/funds/clientreportdetailvo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clientId = data.clientReportDetailVo.clientId
                this.dataForm.clientName = data.clientReportDetailVo.clientName
                this.dataForm.fundsOrderDate = data.clientReportDetailVo.fundsOrderDate
                this.dataForm.accountId = data.clientReportDetailVo.accountId
                this.dataForm.accountName = data.clientReportDetailVo.accountName
                this.dataForm.remainAmount = data.clientReportDetailVo.remainAmount
                this.dataForm.saleOrder = data.clientReportDetailVo.saleOrder
                this.dataForm.saleAmount = data.clientReportDetailVo.saleAmount
                this.dataForm.writtenOffAmount = data.clientReportDetailVo.writtenOffAmount
                this.dataForm.thisRemainAmount = data.clientReportDetailVo.thisRemainAmount
                this.dataForm.thisWrittenOffNum = data.clientReportDetailVo.thisWrittenOffNum
                this.dataForm.writtenOffNum = data.clientReportDetailVo.writtenOffNum
                this.dataForm.remark = data.clientReportDetailVo.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/funds/clientreportdetailvo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'clientId': this.dataForm.clientId,
                'clientName': this.dataForm.clientName,
                'fundsOrderDate': this.dataForm.fundsOrderDate,
                'accountId': this.dataForm.accountId,
                'accountName': this.dataForm.accountName,
                'remainAmount': this.dataForm.remainAmount,
                'saleOrder': this.dataForm.saleOrder,
                'saleAmount': this.dataForm.saleAmount,
                'writtenOffAmount': this.dataForm.writtenOffAmount,
                'thisRemainAmount': this.dataForm.thisRemainAmount,
                'thisWrittenOffNum': this.dataForm.thisWrittenOffNum,
                'writtenOffNum': this.dataForm.writtenOffNum,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
